<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="计划编号" prop="planNum">
        <el-input
          v-model="queryParams.planNum"
          placeholder="请输入计划编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单据编号" prop="docNum">
        <el-input
          v-model="queryParams.docNum"
          placeholder="请输入单据编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['prodplan:planning:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['prodplan:planning:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['prodplan:planning:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleSplit"
          v-hasPermi="['prodplan:planning:split']"
        >拆分</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="multiple"
          @click="handleLocked"
          v-hasPermi="['prodplan:planning:locked']"
        >锁定</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="multiple"
          @click="handleUnLocked"
          v-hasPermi="['prodplan:planning:unlocked']"
        >反锁定</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="multiple"
          @click="handleDispatch"
          v-hasPermi="['prodplan:planning:dispatch']"
        >派工</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-download1"
          size="mini"
          :disabled="single"
          @click="handleImport"
          v-hasPermi="['prodplan:planning:import']"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['prodplan:planning:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="planningList" @selection-change="handleSelectionChange" :default-sort="{prop: 'date', order: 'descending'}" ref="tableDatas">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="计划编号" align="center" prop="planNum" sortable width="150"/>
      <el-table-column label="单据编号" align="center" prop="docNum" sortable width="150"/>
      <el-table-column label="数量" align="center" prop="quantity" sortable width="150"/>
      <el-table-column label="锁定状态" align="center" prop="lockState" :formatter="lockStateFormat" sortable width="150"/>
      <el-table-column label="拆分状态" align="center" prop="splitStatus" :formatter="splitStatusFormat" sortable width="150"/>
      <el-table-column label="产品id" align="center" prop="productId" sortable width="150"/>
      <el-table-column label="产品名称" align="center" prop="productName" sortable width="150"/>
      <el-table-column label="规格型号" align="center" prop="specModel" sortable width="150"/>
      <el-table-column label="产品编码" align="center" prop="productCode" sortable width="150"/>
      <el-table-column label="单位" align="center" prop="unit" sortable width="150"/>
      <el-table-column label="订单类型" align="center" prop="orderType" :formatter="orderTypeFormat" sortable width="150"/>
      <el-table-column label="计划交货日期" align="center" prop="deliveryDate" sortable width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deliveryDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划生产时间" align="center" prop="plannedProdtime" sortable width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.plannedProdtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="派工状态" align="center" prop="dispatchStatus" :formatter="dispatchStatusFormat" sortable width="150"/>
      <el-table-column label="排产类型" align="center" prop="schedulingType" :formatter="schedulingTypeFormat" sortable width="150"/>
      <el-table-column label="车间" align="center" prop="workshop" sortable width="150"/>
      <el-table-column label="产线id" align="center" prop="prodLine" sortable width="150"/>
      <el-table-column label="创建时间" align="center" prop="createTime" sortable width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createUser" sortable width="150"/>
      <el-table-column label="修改时间" align="center" prop="updateTime" sortable width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改人" align="center" prop="updateUser" sortable width="150"/>
      <el-table-column label="备注" align="center" prop="remark" sortable width="150"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['prodplan:planning:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['prodplan:planning:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改排产管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="计划编号" prop="planNum">
          <el-input v-model="form.planNum" placeholder="请输入计划编号" />
        </el-form-item>
        <el-form-item label="单据编号" prop="docNum">
          <el-input v-model="form.docNum" placeholder="请输入单据编号" />
        </el-form-item>
        <el-form-item label="数量" prop="quantity">
          <el-input v-model="form.quantity" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="锁定状态" prop="lockState">
          <el-select v-model="form.lockState" placeholder="请选择锁定状态">
            <el-option
              v-for="dict in lockStateOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="拆分状态" prop="splitStatus">
          <el-select v-model="form.splitStatus" placeholder="请选择拆分状态">
            <el-option
              v-for="dict in splitStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="产品id" prop="productId">
          <el-input v-model="form.productId" placeholder="请输入产品id" />
        </el-form-item>
        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="规格型号" prop="specModel">
          <el-input v-model="form.specModel" placeholder="请输入规格型号" />
        </el-form-item>
        <el-form-item label="产品编码" prop="productCode">
          <el-input v-model="form.productCode" placeholder="请输入产品编码" />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="订单类型" prop="orderType">
          <el-select v-model="form.orderType" placeholder="请选择订单类型">
            <el-option
              v-for="dict in orderTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="计划交货日期" prop="deliveryDate">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.deliveryDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择计划交货日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="计划生产时间" prop="plannedProdtime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.plannedProdtime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择计划生产时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="派工状态" prop="dispatchStatus">
          <el-select v-model="form.dispatchStatus" placeholder="请选择派工状态">
            <el-option
              v-for="dict in dispatchStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排产类型" prop="schedulingType">
          <el-select v-model="form.schedulingType" placeholder="请选择排产类型">
            <el-option
              v-for="dict in schedulingTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车间" prop="workshop">
          <el-input v-model="form.workshop" placeholder="请输入车间" />
        </el-form-item>
        <el-form-item label="产线id" prop="prodLine">
          <el-input v-model="form.prodLine" placeholder="请输入产线id" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPlanning, getPlanning, delPlanning, addPlanning, updatePlanning, exportPlanning, split, locked, unLocked, dispatch, unDispatch } from "@/api/prodplan/planning";

export default {
  name: "Planning",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 排产管理表格数据
      planningList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 锁定状态(0未锁定 1已锁定)字典
      lockStateOptions: [],
      // 拆分状态字典
      splitStatusOptions: [],
      // 订单类型字典
      orderTypeOptions: [],
      // 派工状态字典
      dispatchStatusOptions: [],
      // 排产类型字典
      schedulingTypeOptions: [],
      //存放选中值的数组
      multipleTable:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        planNum: undefined,
        docNum: undefined,
        quantity: undefined,
        lockState: undefined,
        splitStatus: undefined,
        productId: undefined,
        productName: undefined,
        specModel: undefined,
        productCode: undefined,
        unit: undefined,
        orderType: undefined,
        deliveryDate: undefined,
        plannedProdtime: undefined,
        dispatchStatus: undefined,
        schedulingType: undefined,
        workshop: undefined,
        prodLine: undefined,
        createTime: undefined,
        createUser: undefined,
        updateTime: undefined,
        updateUser: undefined,
        isDel: undefined,
        remark: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("prodplan_planning_lock_state").then(response => {
      this.lockStateOptions = response.data;
    });
    this.getDicts("prodplan_planning_split_status").then(response => {
      this.splitStatusOptions = response.data;
    });
    this.getDicts("prodplan_planning_order_type").then(response => {
      this.orderTypeOptions = response.data;
    });
    this.getDicts("prodplan_planning_dispatch_status").then(response => {
      this.dispatchStatusOptions = response.data;
    });
    this.getDicts("prodplan_planning_scheduling_type").then(response => {
      this.schedulingTypeOptions = response.data;
    });
  },
  methods: {
    /** 查询排产管理列表 */
    getList() {
      this.loading = true;
      listPlanning(this.queryParams).then(response => {
        this.planningList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 锁定状态(0未锁定 1已锁定)字典翻译
    lockStateFormat(row, column) {
      return this.selectDictLabel(this.lockStateOptions, row.lockState);
    },
    // 拆分状态字典翻译
    splitStatusFormat(row, column) {
      return this.selectDictLabel(this.splitStatusOptions, row.splitStatus);
    },
    // 订单类型字典翻译
    orderTypeFormat(row, column) {
      return this.selectDictLabel(this.orderTypeOptions, row.orderType);
    },
    // 派工状态字典翻译
    dispatchStatusFormat(row, column) {
      return this.selectDictLabel(this.dispatchStatusOptions, row.dispatchStatus);
    },
    // 排产类型字典翻译
    schedulingTypeFormat(row, column) {
      return this.selectDictLabel(this.schedulingTypeOptions, row.schedulingType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        planningId: undefined,
        planNum: undefined,
        docNum: undefined,
        quantity: undefined,
        lockState: undefined,
        splitStatus: undefined,
        productId: undefined,
        productName: undefined,
        specModel: undefined,
        productCode: undefined,
        unit: undefined,
        orderType: undefined,
        deliveryDate: undefined,
        plannedProdtime: undefined,
        dispatchStatus: undefined,
        schedulingType: undefined,
        workshop: undefined,
        prodLine: undefined,
        createTime: undefined,
        createUser: undefined,
        updateTime: undefined,
        updateUser: undefined,
        isDel: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.planningId)
      this.single = selection.length!==1
      this.multiple = !selection.length
      this.multipleTable = selection;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加排产管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const planningId = row.planningId || this.ids
      getPlanning(planningId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改排产管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.planningId != null) {
            updatePlanning(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPlanning(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const planningIds = row.planningId || this.ids;
      this.$confirm('是否确认删除排产管理编号为"' + planningIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPlanning(planningIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有排产管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportPlanning(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    //拆分按钮
    handleSplit(row){
      this.reset();
      const planningId = row.planningId || this.ids;
    },
    //锁定按钮
    handleLocked(row){
      this.reset();
      const planningId = row.planningId || this.ids;
      locked(planningId).then(response => {
        this.msgSuccess("锁定成功");
        this.getList();
        });
    },
    //反锁定按钮
    handleUnLocked(row){
      this.reset();
      const planningId = row.planningId || this.ids;
      unLocked(planningId).then(response => {
        this.msgSuccess("反锁定成功");
        this.getList();
        });
    },
    //派工按钮
    handleDispatch(){
      var isLock = this.multipleTable.map(item => item.lockedState)[0];
        var dispatchStatus = this.multipleTable.map(item => item.dispatchStatus)[0];
        var data =this.$refs.tableDatas.selection;
        // var tempArray=new Array();
        if (isLock == 0) {
          this.msgSuccess("未锁定状态不能派工");
          return;
        }
        if (dispatchStatus == 1) {
          this.msgSuccess("已排产状态不能派工");
          return;
        }
          dispatch(data).then(response => {
            this.msgSuccess("派工成功");
            this.reset();
            this.getList();
          });
      },
  }
};
</script>
